import { game, RenderRoot2D } from 'cc'
import Singleton from '../base/Singleton'
import { DEFAULT_FADE_DURATION, DrawMgr } from '../ui/DrawMgr'
import { createUiNode } from '../utils'
/** 全局渐入渐出动画 */
export default class FaderMgr extends Singleton {
  static get Instance() {
    return super.getInstance<FaderMgr>()
  }

  private _fader: DrawMgr = null

  get fader(): DrawMgr {
    if (this._fader !== null) {
      return this._fader
    }

    const root = createUiNode()
    root.addComponent(RenderRoot2D)

    const node = createUiNode()
    node.setParent(root)
    this._fader = node.addComponent(DrawMgr)
    this._fader.init()
    game.addPersistRootNode(root)

    return this._fader
  }

  async fadeIn(duration: number = DEFAULT_FADE_DURATION) {
    await this.fader.fadeIn(duration)
  }

  async fadeOut(duration: number = DEFAULT_FADE_DURATION) {
    await this.fader.fadeOut(duration)
  }
}
